/*
 * Copyright 2011 Michael Nitsche
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package de.abcsolutions.g5.tool;

/**
 * @author Michael Nitsche
 * 28.02.2011	Created
 */
public class G5Math {

	/**
	 * ByteToValue(byte value); // converts -128 to 0
	 */
	public static int toValue(byte b) {
		return b & 255;
	}

	/**
	 * byte ValueToByte(int value); // converts 0 to -128
	 */
	public static byte valueToByte(int value) {
		return (byte)value;
	}

	/**
	 * byte setFlag(byte Byte, byte offset, boolean flag); // sets bit at offset to yes/no
	 */
	public static byte setFlag(byte b, byte offset, boolean flag) {
		int bitValue=bitValue(offset);
		int erg=flag
			? b | bitValue
			: (b | bitValue) ^ bitValue;
		return (byte)erg;
	}

	/**
	 * boolean getFlag(byte Byte, byte offset); // gets flag of Byte at offset
	 */
	public static boolean getFlag(byte b, byte offset) {
		int bitValue=bitValue(offset);
		return (b & bitValue) == bitValue;
	}

	// Private

	private static int bitValue(int bit) {
		int x=bit==0 ? 1 : (int)Math.pow(2, bit);
		return x;
	}

}
